# ---- ClearCase history handling ----------------------------

import os, string
from logParser import * 

#TEST	= 1

class CChistory:
	def __init__(self, file):
		self.file = file
		self.description = ''
		self.history = []
		self.revisions = []
		self.nversions = 0
		self.symbolList = []
		self.reversed = FALSE
	def add_symbols(self, labels):
		for label in labels:
			self.symbolList.append((label, self.nversions))
		self.nversions = self.nversions + 1
	def symbollist(self): 
		if not self.reversed:
			self._reverse()
		return self.symbolList
	def _reverse(self):
		symbolList = self.symbolList
		lnr = self.nversions
		self.symbolList = []
		for (label, i) in symbolList:
			self.symbolList.append('%s: 1.%d' % (label, lnr-i-1))
		if lnr>0:
			self.symbolList.append('HEAD: 1.%d' % (lnr-1))
			for rev in self.revisions:
				lnr = lnr - 1
				rev.rev = '1.%d' % lnr
		self.reversed = TRUE

class CClog(Log):
	CMD	= 'cleartool lsh -fmt "%%En||%%e||%%Vn||%%u||%%Vd||%%Nl\\n%%c<c>\\n" %s'
	def __init__(self, file, workFile, opt=''):
		self.files = [] 
		self.index = 0
		Log.__init__(self, file, workFile, opt)
 
	def parse_log(self, log):
		if self.files:
			self.index = self.index+1
		else:
			self.do_parse_all(log)
		self.valid = self.index < len(self.files)
		if not self.valid:
			self.index = 0
			return
		self.description = self.files[self.index].description
		self.history = self.files[self.index].history
		self.revisions = self.files[self.index].revisions
 
	def do_parse_all(self, log):
		self.valid = FALSE
		line = log.readline()
		while line and not string.lstrip(line):
			line = log.readline()
		if not line:
			return
		files = {}
		while line:
#	    if TEST: print line,
			if line[0] in ' \t':
				line = log.readline()
				continue
			line = string.rstrip(line)
			fields = string.split(line, '||')
			comment = self.read_comment(log)
			event = fields[1]
			file = os.path.basename(fields[0])
			if not files.has_key(file):
				files[file] = CChistory(file)
			cchistory = files[file]
			[ver, user, day_date_time] = fields[2:5]
			[day, date, time] = string.split(day_date_time)
			[month, day, year] = string.split(date, '/')
			date = '%s/%s/%s %s;' % (year, month, day, time)
			labels = string.split(fields[5])
			if event == 'create version':
				rev = Revision('')
				rev.author = user + ';'
				rev.date = date
				rev.ccrev = ver
				rev.lines = ''
				rev.labels = labels
				rev.comment = comment
				cchistory.revisions.append(rev)
				cchistory.add_symbols(labels)
				cchistory.history.append(string.join((event, ver, date, user)))
			elif event == 'checkout version':
				cchistory.history.append(string.join((event, date, user)))
			else:
				if event == 'create file element':
					cchistory.description = comment
				cchistory.history.append(string.join((event, ver, date, user)))
			if comment:
				cchistory.history.append(comment)
			cchistory.history.append('----------------')
			line = log.readline()
		for file in files.keys():
			self.files.append(files[file])
 
	def read_comment(self, log):
		comment = ''
		line = log.readline()
		while line <> '<c>\n':
			comment = comment + line
			line = log.readline()
		return string.rstrip(comment)
 
	def parse_history(self):
		return
	def work_file(self):
		file = self.files[self.index].file
		for dirfile in self.workFiles:
			if os.path.basename(dirfile) == file:
				return dirfile
		return file
	def head_rev(self):
		return ''
	def lockers(self):
		return ''
	def symbolstr(self):
		return string.join(self.files[self.index].symbollist(), NL)
	def symbollist(self):
		return self.files[self.index].symbollist()
